<?php

	error_reporting(0);
	session_start();
	error_reporting(1);

	$site_root = "";
	require_once ($site_root . "common/PinSQL.obj");
	$pinSQL = new PinSQL();
	
	$username = $_SESSION['username'];
	
	// automatic user tagging after 5 days
	if ( !$pinSQL->GetUserdataField($username, "autotag_sent") )
	{
		
		$difference = time() - strtotime( $pinSQL->GetUserField($username, "signup_date") );
		$taggedUsers = $pinSQL->GetUserdataField($username, "tagged_users");
		if ( (($difference / 60 / 60 / 24) >= 5) AND (!$taggedUsers) )
		{
			$id = $_SESSION['id'];
			$pinSQL->Query("UPDATE memberdata SET autotag_sent='1' WHERE id='$id'");
			
			$pinSQL->Query("SELECT * FROM members ORDER BY RAND() LIMIT 0, 10");
			
			$taggedUsers = "";
			while ($row = $pinSQL->FetchNextRow())
			{
				if ($taggedUsers)
				{
					$taggedUsers .= ",";
				}
				$taggedUsers .= $row['username'];
			}
			
			$pinSQL->Query("UPDATE memberdata SET tagged_users='$taggedUsers' WHERE id='$id'");
			
			include "send_autotag.php";
		}
	}

	
	
	?>
	
<?php if ($browser=="ie6") { ?>
<style type="text/css">
.profile_box_header
{
	/* fix IE6 border rendering bug */
	border: 1px;
	padding-left: 4px;
	padding-right: 5px;
}
</style>
<?php } ?>

<script type="text/javascript" src="common/highslide/highslide.js"></script>
<link rel="stylesheet" type="text/css" href="common/highslide/highslide.css" />

<script type="text/javascript">    
    hs.graphicsDir = 'common/highslide/graphics/';
    hs.outlineType = 'outer-glow';
</script>

<?php

	$pinSQL->Query("SELECT * FROM messages WHERE touser='$username' AND isread='0' AND archived='0' ORDER BY date");
	
 	if ( $pinSQL->GetNumRows() )
 	{
	 	// user has new messages
?>

<center>
<img src="img/icons/inbox_sm.png" width="16" height="16">
<a href="/members/frame.php?page=inbox">You have <?php echo $pinSQL->GetNumRows(); ?> new unread message<?php echo ($pinSQL->GetNumRows() > 1) ? "s" : ""; ?></a>!
</center><p />

<?php
 	}
 	
 	$birthday = $pinSQL->GetUserdataField( $_SESSION['username'] , "birthday" );
 	
	$arr = explode('-', $birthday);
	$timestamp = mktime(0, 0, 0, $arr[1], $arr[2], $arr[0], -1);
	$today = time();
	if ( date('F j', $timestamp) == date('F j', $today) )
	{
		$isBirthday = TRUE;
	}
 	

?>





<!-- display left column !-->
<div style="float: left; width: 320px;">

<!-- display recent activity !-->
	<div class="profile_box_header">
		Recent Activity
	</div>
	
	<div class="profile_box_border">
		<div class="padded_text">
			
			<img src="/img/arrow_blue.gif" />
			<a href="javascript:ShowHideCommentBox()">Add a news story</a>
			
			<div id="commentdiv" style="display: none;">
			<blockquote>
				<span id="comment_status" style="display: none; font-size: 9pt;">Please wait...<img src="/img/rotating_arrow.gif" style="position: relative; top: 4px;"></span>
				
					<table>
						<tr>
							<td>Title:</td>
							<td><input type="text" name="title" id="title" size="20" value=""></td>
						</tr>
						
						<tr>
							<td>Description:</td>
							<td><textarea name="comment" id="comment" rows=3 cols=20></textarea></td>
						</tr>
						
						<tr>
							<td></td>
							<td><img src="/img/arrow_green.gif" /><a href="javascript:PostComment();"> Post Story</a></td>
						</tr>
					</table>
					
			</blockquote>
			</div>
			
			<div class="horizontal_separation"></div>
			
			<div class="subtle">
				Showing 15 most recent events from your tagged users.
			</div>
			
			<div class="horizontal_separation"></div>
			
			<div id="dynamic_content">
				<?php
				$site_root = ""; 
				include "gen_activity.php"; ?>
			</div>
			
		</div>
	</div>
	
	<div class="horizontal_separation" style="clear: both;"></div>
	<div class="horizontal_separation" style="clear: both;"></div>
	
	<img src="/img/copyright_footer_blue.png" />

	<div class="horizontal_separation" style="clear: both;"></div>
	<div class="horizontal_separation" style="clear: both;"></div>
	
</div>

<!-- display right column !-->
<div style="float: right; width: 320px;">

<?php if ($isBirthday) { ?>
	<div class="profile_box_border" style="border: 2px solid rgb(200, 70, 200);">
		<div class="padded_text">
			<span class="accent">Happy birthday, <?php echo $pinSQL->GetFirstName( $_SESSION['username'] ); ?>!</span>
			We at the Pin Project hope you have a fantastic day =)
		</div>
	</div>
	
	<div class="horizontal_separation"></div>
<?php } ?>
	
	<div class="profile_box_header">
		Picture Spotlight
	</div>
	
	<div class="profile_box_border">
		<div class="padded_text">
<?php

	$pinSQL->Query("SELECT * FROM picturedata ORDER BY RAND() LIMIT 1, 1");
	$row = $pinSQL->FetchNextRow();
	
	$picID = $row['id'];

		$imageSize = getimagesize("members/userData/" . $row['artist'] . "/pictures/" . $row['thumburl']);
		
		if (($imageSize[0] <= 150) AND ($imageSize[1] <= 150))
		{
			$newWidth = $imageSize[0];
			$newHeight = $imageSize[1];
		} else {
			$heightFactor = $imageSize[1] / $imageSize[0];
			$newWidth = 150;
			$newHeight = $newWidth * $heightFactor;
			
			if ($newHeight > 150)
			{
				$widthFactor = $imageSize[0] / $imageSize[1];
				$newHeight = 150;
				$newWidth = $newHeight * $widthFactor;
			}
		}
		
		$realname = $pinSQL->GetUserField($row['artist'], "firstname") . " " . $pinSQL->GetUserField($row['artist'], "lastname");
		if ($realname == " ")
			$realname = $row['artist'];
		
		$averagestars = round($row['totalstars'] / $row['totalratings'] / 2, 0);
		
		$pinSQL->Query("SELECT * FROM picturecomments WHERE itemid=$picID ORDER BY RAND()");
		

?>
		
			<table><tr><td>
	
			<a href="members/userData/<?php echo $row['artist'] . "/pictures/" . $row['filename']; ?>" class="highslide" onclick="return hs.expand(this)">
			    <img src="members/userData/<?php echo $row['artist'] . "/pictures/" . $row['thumburl']; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" border=0>
			</a>
			
			</td><td width="4">&nbsp;</td>
			<td><a href="/common/gallery/portal_pictures.php?dest=<?php echo $row['id']; ?>"><?php echo ($row['name']) ? $row['name'] : "(untitled)"; ?></a>
			<?php if ($averagestars) { ?><img src="/img/gauge_<?php echo $averagestars; ?>_sm.png" /><?php } ?>
			<div class="subtle">by <a href="members/frame.php?page=profile&user=<?php echo $row['artist']; ?>"><?php echo $realname; ?></a></div>
			<p /><?php echo nl2br($row['description']); ?></td>
			</tr></table>
			
			<?php if ( $pinSQL->GetNumRows() ) {
				$rowC = $pinSQL->FetchNextRow(); ?>
			<div class="accent">
				"<?php echo nl2br($rowC['comment']); ?>" <span class="subtle">-<?php echo $pinSQL->GetFirstName($rowC['visitor']); ?></span>
			</div>
			<?php } ?>
		
		</div>
	</div>
	
	<div class="horizontal_separation"></div>

	<div class="profile_box_header">
		Pin Project Highlights
	</div>
	
	<div class="profile_box_border">
		<div class="padded_text">
		
<?php

	$pinSQL->Query("SELECT * FROM activity");
	
	$total = $pinSQL->GetNumRows();
	
	while ( $row = $pinSQL->FetchNextRow() )
	{
		$events[ $row['by_user'] ]++;
	}
	
	arsort( $events );
	$mostactive = NULL;
	
	$i=0;
	
	foreach( $events as $user => $count )
	{
		if (!$i)
		{
			$max = $count;
			$mostactive = $user;
		}
		
		$i++;
	}

	$difference = $max - $events[$_SESSION['username']];
					
?>
<table>
<tr>

<?php
		$picurl = $pinSQL->GetUserdataField($mostactive, "picurl");
		
		if ($picurl)
		{
		$imageSize = getimagesize("members/userData/$mostactive/$picurl");
		
		if ($imageSize[0] <= 80)
		{
			$newWidth = $imageSize[0];
			$newHeight = $imageSize[1];
		} else {
			$heightFactor = $imageSize[1] / $imageSize[0];
			$newWidth = 80;
			$newHeight = $newWidth * $heightFactor;
		}
		
?>
		<td><img src="members/userData/<?php echo $mostactive . "/" . $picurl; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" /></td>
<?php
		}
?>
		
			<td width="4">&nbsp;</td><td><div class="redtext">Most active user:</div>
			<div class="accent"><?php echo $pinSQL->GetUserField($mostactive, "firstname") . " " . $pinSQL->GetUserField($mostactive, "lastname"); ?></div>
			<a href="members/frame.php?page=profile&user=<?php echo $mostactive; ?>"><?php echo $mostactive; ?></a>
			</td>
			</tr><tr><td colspan="3">
				<img src="/img/arrow_red.gif" /> <a href="frame.php?page=mostactive.php" />View most active users</td></tr></table>
			
		</div>
	</div>
	
	<div class="horizontal_separation"></div>
	
	<div class="profile_box_header">
		Bulletin Posts
	</div>
	
	<div class="profile_box_border">
		<div class="padded_text">
		
			<div id="bulletin">
				<?php
				$site_root = ""; 
				include "gen_bulletin.php"; 
				?>
			</div>
			
		</div>
	</div>
	
	<div class="horizontal_separation" style="clear: both;"></div>
	<div class="horizontal_separation" style="clear: both;"></div>
		
</div>


<div class="right">
	<div class="right_title">User Status</div>
	
	<div class="horizontal_separation"></div>
	
	<div class="menu_text">
<?php if (!$_SESSION['username']) { ?>
		Not signed in.
<?php } else { ?>

<?php
	$id = $_SESSION['id'];
	$pinSQL->Query("SELECT * FROM memberdata WHERE id='$id'");
	$row = $pinSQL->FetchNextRow();
	$picurl = $row['picurl'];
	
	$tWidth = 80;
	
	if ($picurl)
	{
		$imageSize = getimagesize("members/userData/$username/$picurl");
		
		if ($imageSize[0] <= $tWidth)
		{
			$newWidth = $imageSize[0];
			$newHeight = $imageSize[1];
		} else {
			$heightFactor = $imageSize[1] / $imageSize[0];
			$newWidth = $tWidth;
			$newHeight = $newWidth * $heightFactor;
		}
		
?>
		<img src="/members/userData/<?php echo $username . "/" . $picurl; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" />	
<?php

	}
	else
	{
		
?>
		<img src="/img/icons/nopic.png" width="<?php echo $tWidth; ?>" height="<?php echo $tWidth; ?>" />
		<br /><img src="/img/arrow_green.gif" /><a href="/members/frame.php?page=editProfile">Add a user picture</a>!
		<p />
<?php
	}
?>

		Signed in as:
		<br /><span class="redtext"><?php echo $_SESSION['username']; ?></span>
<?php } ?>
	</div>
	
	<div class="horizontal_separation"></div>
</div>



<script type="text/javascript">

	function ShowHideCommentBox()
	{
          commentBlock = document.getElementById('commentdiv');
          
          if (commentBlock.style.display == 'block')
          {
               commentBlock.style.display = 'none';
          } else {
               commentBlock.style.display = 'block';
          }
	}
	
	function PostComment()
	{
		statusBlock = document.getElementById('comment_status');
		statusBlock.style.display = 'block';
		
		<?php include "common/ajax_init.php"; ?>
		
		request.open("GET", 'do_action.php?actionname=poststory&title='
		+ document.getElementById('title').value
		+ '&message=' + document.getElementById('comment').value, true);
          
		request.onreadystatechange = function()
		{
			if(request.readyState == 4)
			{
				var responseText = request.responseText;
				statusBlock.style.display = 'none';
				commentBlock.style.display = 'none';
				document.getElementById('dynamic_content').innerHTML = responseText;
			}
		}

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send(null); 
	}
     
	<?php include "common/PinAJAX.php"; ?>
	
</script>